package com.jqq.outstream_;

import org.junit.jupiter.api.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author 姬倩倩
 * @version 1.0
 */
public class FileOutputStream_ {
    public static void main(String[] args) {

    }
    //将数据写到文件中
    //如果文件不存在，则创建文件

    @Test
    public void writeFile(){

        //创建FileOutputStream对象
        String filePath = "d:\\a.txt";
        FileOutputStream fileOutputStream = null;

        try {
            //1.new FileOutputStream(filePath)会覆盖原来的内容
            //2.new FileOutputStream(filePath, true)会保留原来的内容，在后边追加
            //得到FileOutputStream对象
            fileOutputStream = new FileOutputStream(filePath);
            //写入一个字节
            fileOutputStream.write('a');
            //写入字符串
            String str = "hello";
            fileOutputStream.write(str.getBytes());//把字符串转换成字节写入
            fileOutputStream.write(str.getBytes(), 0, str.length());
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
